﻿using Architecture.Domain.Services.Interface;
using Architecture.Domain.TraceabilityManage;
using Architecture.Domian.DTO;
using Architecture.Repository.Interface;
using System.Runtime.ConstrainedExecution;
using System.Text.RegularExpressions;

namespace Architecture.Domain.Services.Impl
{
    public class CultureService : ICultureService
    {
        private readonly ICultureRepository _cultureRepository;
        private readonly ICorporationRepository _corporationRepository;
        private readonly IImmuneInformationRepository _immuneInformationRepository;

        public CultureService(
            ICultureRepository cultureRepository,
            ICorporationRepository corporationRepository,
            IImmuneInformationRepository immuneInformationRepository)
        {
            _cultureRepository = cultureRepository;
            _corporationRepository = corporationRepository;
            _immuneInformationRepository = immuneInformationRepository;
        }

        public async Task<int> AddCultures(Culture culture)
        {
            return await _cultureRepository.AddAsync(culture);
        }

        public async Task<int> BatchCorporationPicList(string? productionLot, string? urlImg, int cId)
        {
            var corporation = await _corporationRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete);

            if (corporation == null)
            {
                await AddCorporation(new Corporation { ProductionLot = productionLot });

                corporation = await _corporationRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete);

                if (cId == 0)
                {
                    await AddCultures(new Culture { ProductionLot = productionLot, FarmId = corporation.CId });
                }

                corporation.CompanyPicture = urlImg;
                return await _corporationRepository.UpdateAsync(corporation);
            }
            else
            {
                corporation.CompanyPicture = urlImg;
                return await _corporationRepository.UpdateAsync(corporation);
            }
        }

        public async Task<int> BatchCulturePicList(string? productionLot, string? urlImg)
        {
            var culture = await _cultureRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete);

            if (culture == null)
            {
                Culture culture1 = new Culture
                {
                    ProductionLot = productionLot,
                    DynamicVerification = urlImg,
                };
                return await AddCultures(culture1);
            }
            else
            {
                culture.DynamicVerification = urlImg;

                return await _cultureRepository.UpdateAsync(culture);
            }
        }

        public async Task<List<PictureDTO>> GetCorporationPicture(string? productionLot, int parmId)
        {
            var corporation = await _corporationRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete && x.CId == parmId);

            if (corporation == null || string.IsNullOrEmpty(corporation.CompanyPicture))
            {
                return new List<PictureDTO>();
            }

            return corporation.CompanyPicture.Split(',').Select(x => new PictureDTO
            {
                name = x,
                url = "http://localhost:5124/Images/" + x
            }).ToList();
        }

        public async Task<List<PictureDTO>> GetCulturePicture(string? productionLot)
        {
            var culture = await _cultureRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete);

            if (culture == null || string.IsNullOrEmpty(culture.DynamicVerification))
            {
                return new List<PictureDTO>();
            }

            return culture.DynamicVerification.Split(',').Select(x => new PictureDTO
            {
                name = x,
                url = "http://localhost:5124/Images/" + x
            }).ToList();
        }

        public async Task<Culture> GetCultureInfo(string? productionLot)
        {
            return await _cultureRepository.GetByFuncAsync(x => x.ProductionLot == productionLot);
        }

        public async Task<int> UpdateCultures(Culture culture)
        {
            return await _cultureRepository.UpdateAsync(culture);
        }

        public async Task<int> AddCorporation(Corporation corporation)
        {
            var culture = await _cultureRepository.GetByFuncAsync(x => x.ProductionLot == corporation.ProductionLot && !x.IsDelete);

            if (culture == null)
            {
                await _corporationRepository.AddAsync(corporation);

                Culture culture1 = new Culture
                {
                    FarmId = corporation.CId,
                    ProductionLot = corporation.ProductionLot
                };
                return await AddCultures(culture1);
            }

            await _corporationRepository.AddAsync(corporation);

            culture.FarmId = corporation.CId;

            return await _cultureRepository.UpdateAsync(culture);
        }

        public async Task<Corporation> GetCorporationInfo(string? productionLot, int parmId)
        {
            return await _corporationRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete && x.CId == parmId);
        }

        public async Task<int> UpdateCorporation(Corporation corporation)
        {
            var culture = await _cultureRepository.GetByFuncAsync(x => x.ProductionLot == corporation.ProductionLot && !x.IsDelete);

            if (culture == null)
            {
                Culture culture1 = new Culture
                {
                    FarmId = corporation.CId,
                    ProductionLot = corporation.ProductionLot
                };
                return await AddCultures(culture1);
            }

            culture.FarmId = corporation.CId;

            await UpdateCultures(culture);

            return await _corporationRepository.UpdateAsync(corporation);
        }

        public async Task<int> AddImmuneInFormation(ImmuneInFormation immune)
        {
            return await _immuneInformationRepository.AddAsync(immune);
        }

        public async Task<int> UpdateImmuneInFormation(ImmuneInFormation immune)
        {
            return await _immuneInformationRepository.UpdateAsync(immune);
        }

        public async Task<ImmuneInFormation> GetImmuneInFormationInfo(int iId)
        {
            return await _immuneInformationRepository.GetByIdAsync(iId);
        }

        public async Task<List<ImmuneInFormation>> GetImmuneInFormationList(string? productionLot)
        {
            if (string.IsNullOrEmpty(productionLot)) return null;
            return await _immuneInformationRepository.GetAsync(x => x.ProductionLot == productionLot);
        }

        public async Task<int> DeleteImmuneInFormation(int iId)
        {
            var result = await _immuneInformationRepository.GetByIdAsync(iId);
            return await _immuneInformationRepository.DeleteAsync(result);
        }
    }
}
